﻿namespace WMS6.Web
{
    using System;
    using System.IO;
    using System.Management;
    using System.Text;

    public sealed class LicenseUtils
    {
        private static string _systemKey = null;
        private static object _systemKeySync = new object();

        private LicenseUtils()
        {
        }

        private static string GenerateBIOSId()
        {
            return ReadWMIInfo("Win32_BIOS", "SerialNumber");
        }

        private static string GenerateComputerFingerprint()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                builder.Append(GenerateCPUId());
            }
            catch (Exception)
            {
            }
            try
            {
                builder.Append("|");
                builder.Append(GenerateBIOSId());
            }
            catch (Exception)
            {
            }
            try
            {
                builder.Append("|");
                builder.Append(GenerateMotherboardId());
            }
            catch (Exception)
            {
            }
            try
            {
                builder.Append("|");
                builder.Append(GenerateDiskId());
            }
            catch (Exception)
            {
            }
            return builder.ToString();
        }

        private static string GenerateCPUId()
        {
            string str = ReadWMIInfo("Win32_Processor", "UniqueId");
            if (str.Length != 0)
            {
                return str;
            }
            str = ReadWMIInfo("Win32_Processor", "ProcessorId");
            if (str.Length != 0)
            {
                return str;
            }
            str = ReadWMIInfo("Win32_Processor", "Name");
            if (str.Length == 0)
            {
                str = ReadWMIInfo("Win32_Processor", "Manufacturer");
            }
            return (str + ReadWMIInfo("Win32_Processor", "MaxClockSpeed"));
        }

        private static string GenerateDiskId()
        {
            string pathRoot = Path.GetPathRoot(Environment.SystemDirectory);
            if (pathRoot.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                pathRoot = pathRoot.Substring(0, pathRoot.Length - 1);
            }
            ManagementObject obj2 = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + pathRoot + "\"");
            obj2.Get();
            return obj2["VolumeSerialNumber"].ToString();
        }

        private static string GenerateMotherboardId()
        {
            return ReadWMIInfo("Win32_BaseBoard", "SerialNumber");
        }

        public static string GenerateSystemKey()
        {
            lock (_systemKeySync)
            {
                if (_systemKey == null)
                {
                    _systemKey = GenerateComputerFingerprint();
                }
                return _systemKey;
            }
        }

        private static string ReadWMIInfo(string wmiClass, string wmiProperty)
        {
            ManagementClass class2 = new ManagementClass(wmiClass);
            foreach (ManagementObject obj2 in class2.GetInstances())
            {
                try
                {
                    return obj2[wmiProperty].ToString().Trim();
                }
                catch
                {
                    continue;
                }
            }
            return string.Empty;
        }
    }
}

